'''
Created on Apr 20, 2011

@author: riche
'''
import unittest, filecmp, shutil, os
from ft.apps.webserver import server, constants, login_server_app
from ft.framework import wrapper

class Test(unittest.TestCase):
    
    in_tpl_R = ('incoming', constants.d_path('CLIENT_IN_R.txt'))
    in_tpl_WR = ('incoming', constants.d_path('CLIENT_IN_WR.txt'))
    out_tpl_R = ('outgoing', constants.d_path('CLIENT_OUT_R.txt'))
    out_tpl_WR = ('outgoing', constants.d_path('CLIENT_OUT_WR.txt'))
    in_tpl_LR = ('incoming', constants.d_path('CLIENT_IN_LR.txt'))
    in_tpl_LWR = ('incoming', constants.d_path('CLIENT_IN_LWR.txt'))
    out_tpl_LR = ('outgoing', constants.d_path('CLIENT_OUT_LR.txt'))
    out_tpl_LWR = ('outgoing', constants.d_path('CLIENT_OUT_LWR.txt'))
    
    def setUp(self):
        shutil.copy2(constants.d_path('test_file_0.txt'), constants.d_path('test_file_1.txt'))
        if os.path.isfile(Test.out_tpl_R[1] + '_TEST.txt'):
            os.remove(Test.out_tpl_R[1] + '_TEST.txt')
        if os.path.isfile(Test.out_tpl_WR[1] + '_TEST.txt'):
            os.remove(Test.out_tpl_WR[1] + '_TEST.txt')
        
    def test_ss_r(self):
        '''Test the simple server implementation with a read command'''
        ss_abs = server.ServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_R], [Test.out_tpl_R])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_R[1], Test.out_tpl_R[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_R[1] + '_TEST.txt', Test.out_tpl_R[1]))
    
    def test_ss_wr(self):
        '''Test the simple server implementation with a write followed by a read'''
        ss_abs = server.ServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_WR], [Test.out_tpl_WR])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_WR[1], Test.out_tpl_WR[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_WR[1] + '_TEST.txt', Test.out_tpl_WR[1]))

    def test_mrs_r(self):
        '''Test the map-reduce server implementation with a read command'''
        ss_abs = server.MRServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_R], [Test.out_tpl_R])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_R[1], Test.out_tpl_R[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_R[1] + '_TEST.txt', Test.out_tpl_R[1]))
    
    def test_mrs_wr(self):
        '''Test the map-reduce server implementation with a write followed by a read'''
        ss_abs = server.MRServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_WR], [Test.out_tpl_WR])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_WR[1], Test.out_tpl_WR[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_WR[1] + '_TEST.txt', Test.out_tpl_WR[1]))

    def test_ls_r(self):
        '''Test the login server implementation with a read command'''
        ss_abs = login_server_app.LoginServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_LR], [Test.out_tpl_LR])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_LR[1], Test.out_tpl_LR[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_LR[1] + '_TEST.txt', Test.out_tpl_LR[1]))
    
    def test_ls_wr(self):
        '''Test the login server implementation with a write followed by a read'''
        ss_abs = login_server_app.LoginServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_LWR], [Test.out_tpl_LWR])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_LWR[1], Test.out_tpl_LWR[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_LWR[1] + '_TEST.txt', Test.out_tpl_LWR[1]))

    def test_lmrs_r(self):
        '''Test the login map-reduce server implementation with a read command'''
        ss_abs = login_server_app.LoginMRServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_LR], [Test.out_tpl_LR])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_LR[1], Test.out_tpl_LR[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_LR[1] + '_TEST.txt', Test.out_tpl_LR[1]))
    
    def test_lmrs_wr(self):
        '''Test the login map-reduce server implementation with a write followed by a read'''
        ss_abs = login_server_app.LoginMRServerAbs('Server_Alg0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(ss_abs, [Test.in_tpl_LWR], [Test.out_tpl_LWR])
        wp.run()
        ret = filecmp.cmp(Test.out_tpl_LWR[1], Test.out_tpl_LWR[1] + '_TEST.txt', False)
        self.assertEqual(ret, True, '{} is not equal to correct {}'.format(Test.out_tpl_LWR[1] + '_TEST.txt', Test.out_tpl_LWR[1]))
        
    def test_map(self):
        '''Test the map-reduce functionality using the WR inputs'''
        mp = server.MapComp('Map_abs0')
        wp = wrapper.Wrapper()
        wp.add_abstraction(mp, [('in', constants.d_path('i_reader-out_o_Map_abs0-in_u_R.txt'))], 
                           [('out0', constants.d_path('i_Map_abs0-out0_o_Server_abs0(Server_alg1)-incoming_u_R.txt')), 
                            ('out1', constants.d_path('i_Map_abs0-out1_o_Server_abs0(Server_alg2)-incoming_u_R.txt'))])
        wp.run()
        ret1 = filecmp.cmp(constants.d_path('i_Map_abs0-out0_o_Server_abs0(Server_alg1)-incoming_u_R.txt'),
                           constants.d_path('i_Map_abs0-out0_o_Server_abs0(Server_alg1)-incoming_u_R.txt') + "_TEST.txt")
        ret2 = filecmp.cmp(constants.d_path('i_Map_abs0-out1_o_Server_abs0(Server_alg2)-incoming_u_R.txt'),
                           constants.d_path('i_Map_abs0-out1_o_Server_abs0(Server_alg2)-incoming_u_R.txt') + "_TEST.txt")
        self.assertEqual(ret1, True)
        self.assertEqual(ret2, True)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_mrs_r']
    unittest.main()